/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elementsDefense.enemies;

import com.jme3.asset.AssetManager;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Cylinder;
import com.jme3.scene.shape.Sphere;
import com.jme3.util.TangentBinormalGenerator;
import elementsDefense.Main;
import elementsDefense.map.Tile;
import elementsDefense.towers.projectils.Canonball;
import elementsDefense.towers.projectils.Projectil;
import javax.crypto.Mac;

/**
 *
 * @author Ricardo
 */
public class BasicEnemy {
    static Material matEnemy;
    static Material matEnemy_Frozen;
    Spatial geom;
    NavigationControl navigationControl;
    HealthControl health;
    HoverControl hover;
    
    public void build(AssetManager assetManager, Node node){
        if(matEnemy==null){
            matEnemy = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
            matEnemy.setTexture("ColorMap", assetManager.loadTexture("Textures/minion.jpg"));
            
            matEnemy_Frozen = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
            matEnemy_Frozen.setTexture("ColorMap", assetManager.loadTexture("Textures/minion_Frozen.jpg"));
        }
        geom = assetManager.loadModel("Models/minion.j3o");
//        geom.rotate(0, FastMath.HALF_PI, 0);
        geom.setMaterial(matEnemy);
        node.attachChild(geom);
        navigationControl = new NavigationControl(this);
        health = new HealthControl(assetManager,node,this);
        hover = new HoverControl(0.5f, 0.1f, 0.05f);
        geom.addControl(navigationControl);
        geom.addControl(health);
        geom.addControl(hover);
        
        float var=(float) ((Math.random()-0.5)/5);
        Tile start = Main.map.getStarts().getFirst();
        geom.setLocalTranslation(start.x+0.5f, 0, start.y+0.5f+var);
    }

    void sucess() {
        remove();
        Main.player.removeHitpoints(1);
    }
    
    void failure() {
        remove();
        Main.player.addGold(10);
    }
    
    private void remove(){
        geom.removeFromParent();
        EnemyWave.remove(this);
        navigationControl.setEnabled(false);
        geom.removeControl(navigationControl);
        health.setEnabled(false);
        geom.removeControl(health);
        hover.setEnabled(false);
        geom.removeControl(hover);
    }

    public Vector3f getLocation() {
        return geom.getLocalTranslation();
    }

    public void hit(Projectil projectil) {
        health.subtract(projectil.damage);
        if(projectil.slow>0){
            navigationControl.setVelocity(projectil.slow);
            geom.setMaterial(matEnemy_Frozen);
        }
    }
    
    public boolean isAlive(){
        return health.currentHealth>0;
    }
    
    
}
